package oneroom.user.addSeach;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import oneroom.admin.oneroom.roomModel;
import oneroom.comm.IbatisLoader;

import com.ibatis.sqlmap.client.SqlMapClient;

public class addressDao {
	private IbatisLoader 	iBatis;
	private SqlMapClient 	sqlMapper;
	
	public addressDao(){
		this.sqlMapper = iBatis.setConfig();
	}

	public List getAddress(String roomkind, //임대종류
							String charter, //임대형태
							String zipcode2, //지역 도
							String zipcode3, //지역 시
							String[] dong, //지역 읍면동
							String roommoney, // 전세금
							String deposit, // 보증금
							String month,//월세금
							String sort,
							int selectPage,
							int pageSize){ 
		List returnValue = new ArrayList();
		String roommoney1 = "";
		String deposit1 = ""; 
		String month1 = "";
		if(!roommoney.equals("")){
			if(roommoney.equals("2000")){
				roommoney = "2000"; //전세금 검색	
				roommoney1 = "3000"; //전세금 검색	
			}else if(roommoney.equals("3000")){
				roommoney = "3000"; //전세금 검색	
				roommoney1 = "5000"; //전세금 검색	
			}else if(roommoney.equals("5000")){
				roommoney = "5000"; //전세금 검색	
				roommoney1 = "7000"; //전세금 검색	
			}else if(roommoney.equals("7000")){
				roommoney = "7000"; //전세금 검색	
				roommoney1 = "10000"; //전세금 검색	
			}else{
				roommoney = "10000"; //전세금 검색	
				roommoney1 = "1000000000"; //전세금 검색	
			}
		}
		if(!deposit.equals("")){
			if(deposit.equals("100")){
				deposit = "0"; //보증금 검색			
				deposit1 = "100"; //보증금 검색			
			}else if(deposit.equals("200")){
				deposit = "200"; //보증금 검색	
				deposit1 = "200"; //보증금 검색	
			}else if(deposit.equals("300")){
				deposit = "300"; //보증금 검색	
				deposit1 = "300"; //보증금 검색	
			}else if(deposit.equals("500")){
				deposit = "500"; //보증금 검색	
				deposit1 = "500"; //보증금 검색	
			}else if(deposit.equals("1000")){
				deposit = "1000"; //보증금 검색	
				deposit1 = "1000"; //보증금 검색	
			}else if(deposit.equals("1500")){
				deposit = "1500"; //보증금 검색	
				deposit1 = "1500"; //보증금 검색	
			}else if(deposit.equals("2000")){
				deposit = "2000"; //보증금 검색	
				deposit1 = "3000"; //보증금 검색	
			}else if(deposit.equals("3000")){
				deposit = "3000"; //보증금 검색	
				deposit1 = "4000"; //보증금 검색	
			}else if(deposit.equals("5000")){
				deposit = "5000"; //보증금 검색	
				deposit1 = "10000"; //보증금 검색	
			}else if(deposit.equals("10000")){		
				deposit = "10000"; //보증금 검색	
				deposit1 = "1000000000"; //보증금 검색	
			}
		}
		if(!month.equals("")){
			if(month.equals("0")){
				month = "0"; //월세금 검색			
				month1 = "10"; //월세금 검색			
			}else if(month.equals("10")){
				month = "10"; //월세금 검색	
				month1 = "30"; //월세금 검색	
			}else if(month.equals("30")){
				month = "30"; //월세금 검색	
				month1 = "50"; //월세금 검색		
			}else if(month.equals("50")){
				month = "50"; //월세금 검색	
				month1 = "1000000000"; //월세금 검색			
			}
		}
		/* 
		 * 페이징 관련
		 */
		if (selectPage == 0) selectPage = 1;
		if (pageSize == 0) pageSize = 10;
		int startPos = ((selectPage-1)*pageSize);
		
		if(roomkind.equals("")){
			roomkind = null;
		}
		HashMap param = new HashMap();
		param.put("roomkind", roomkind);
		param.put("charter", charter);
		param.put("zipcode2", zipcode2);
		param.put("zipcode3", zipcode3);
		param.put("dong", dong);
		if(roommoney.equals("")){
			param.put("roommoney", roommoney);
			param.put("roommoney1", roommoney1);
		}else{
			param.put("roommoney", Integer.parseInt(roommoney));
			param.put("roommoney1", Integer.parseInt(roommoney1));
		}
		if(deposit.equals("")){
			param.put("deposit", deposit);
			param.put("deposit1", deposit1);
		}else{
			param.put("deposit", Integer.parseInt(deposit));
			param.put("deposit1", Integer.parseInt(deposit1));
		}
		if(month.equals("")){
			param.put("month", month);
			param.put("month1", month1);
		}else{
			param.put("month", Integer.parseInt(month));
			param.put("month1", Integer.parseInt(month1));
		}		
		param.put("sort", sort);
		param.put("startPos", startPos);
		param.put("pageSize", pageSize);
		try{
			returnValue = (List)sqlMapper.queryForList("address.addressseche",param);
		}catch (Exception e) {
			// TODO: handle exception
		}
		return returnValue;
		
	}
	public int getCount(String roomkind, //임대종류
						String charter, //임대형태
						String zipcode2, //지역 도
						String zipcode3, //지역 시
						String[] dong, //지역 읍면동
						String roommoney, // 전세금						
						String deposit, // 보증금
						String month){
		int returnValue = 0;
		String roommoney1 = "";
		String deposit1 = ""; 
		String month1 = "";
		if(!roommoney.equals("")){
			if(roommoney.equals("2000")){
				roommoney = "2000"; //전세금 검색	
				roommoney1 = "3000"; //전세금 검색	
			}else if(roommoney.equals("3000")){
				roommoney = "3000"; //전세금 검색	
				roommoney1 = "5000"; //전세금 검색	
			}else if(roommoney.equals("5000")){
				roommoney = "5000"; //전세금 검색	
				roommoney1 = "7000"; //전세금 검색	
			}else if(roommoney.equals("7000")){
				roommoney = "7000"; //전세금 검색	
				roommoney1 = "10000"; //전세금 검색	
			}else{
				roommoney = "10000"; //전세금 검색	
				roommoney1 = "1000000000"; //전세금 검색	
			}
		}
		if(!deposit.equals("")){
			if(deposit.equals("100")){
				deposit = "0"; //보증금 검색			
				deposit1 = "100"; //보증금 검색			
			}else if(deposit.equals("200")){
				deposit = "200"; //보증금 검색	
				deposit1 = "200"; //보증금 검색	
			}else if(deposit.equals("300")){
				deposit = "300"; //보증금 검색	
				deposit1 = "300"; //보증금 검색	
			}else if(deposit.equals("500")){
				deposit = "500"; //보증금 검색	
				deposit1 = "500"; //보증금 검색	
			}else if(deposit.equals("1000")){
				deposit = "1000"; //보증금 검색	
				deposit1 = "1000"; //보증금 검색	
			}else if(deposit.equals("1500")){
				deposit = "1500"; //보증금 검색	
				deposit1 = "1500"; //보증금 검색	
			}else if(deposit.equals("2000")){
				deposit = "2000"; //보증금 검색	
				deposit1 = "3000"; //보증금 검색	
			}else if(deposit.equals("3000")){
				deposit = "3000"; //보증금 검색	
				deposit1 = "4000"; //보증금 검색	
			}else if(deposit.equals("5000")){
				deposit = "5000"; //보증금 검색	
				deposit1 = "10000"; //보증금 검색	
			}else if(deposit.equals("10000")){		
				deposit = "10000"; //보증금 검색	
				deposit1 = "1000000000"; //보증금 검색	
			}
		}
		if(!month.equals("")){
			if(month.equals("0")){
				month = "0"; //월세금 검색			
				month1 = "10"; //월세금 검색			
			}else if(month.equals("10")){
				month = "10"; //월세금 검색	
				month1 = "30"; //월세금 검색	
			}else if(month.equals("30")){
				month = "30"; //월세금 검색	
				month1 = "50"; //월세금 검색		
			}else if(month.equals("50")){
				month = "50"; //월세금 검색	
				month1 = "1000000000"; //월세금 검색			
			}
		}
		HashMap param = new HashMap();
		param.put("roomkind", roomkind);
		param.put("charter", charter);
		param.put("zipcode2", zipcode2);
		param.put("zipcode3", zipcode3);
		param.put("dong", dong);
		if(roommoney.equals("")){
			param.put("roommoney", roommoney);
			param.put("roommoney1", roommoney1);
		}else{
			param.put("roommoney", Integer.parseInt(roommoney));
			param.put("roommoney1", Integer.parseInt(roommoney1));
		}
		if(deposit.equals("")){
			param.put("deposit", deposit);
			param.put("deposit1", deposit1);
		}else{
			param.put("deposit", Integer.parseInt(deposit));
			param.put("deposit1", Integer.parseInt(deposit1));
		}
		if(month.equals("")){
			param.put("month", month);
			param.put("month1", month1);
		}else{
			param.put("month", Integer.parseInt(month));
			param.put("month1", Integer.parseInt(month1));
		}		

		try{
			returnValue =  ((Integer)sqlMapper.queryForObject("address.addresscount",param)).intValue();
		}catch (Exception e) {
			// TODO: handle exception
		}
		return returnValue;
		
	}
	
	public roomModel getView(String oneroom_seq){
		roomModel returnValue = new roomModel();
		try{
			returnValue = (roomModel)sqlMapper.queryForObject("address.addressview", oneroom_seq);
		}catch (Exception e) {
			// TODO: handle exception
		}
		return returnValue;
	}

}
